home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / src / stab.ecoff.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  3KB  |  107 lines

  1. /* On the SGI, <a.out.h> includes a file that defines a variable named
  2.  * auxtemp.  This causes the linker to complain about this variable
  3.  * being multiply defined, because <a.out.h> was already included by
  4.  * load.vanilla.c.
  5.  */
  6. #define _auxtemp Auxtemp
  7.  
  8. #include <a.out.h>
  9. #include <sys/file.h>
  10.  
  11. #define READ(fd, buf, size, errmsg)\
  12.     if (read (fd, buf, size) != size)\
  13.         Primitive_Error (errmsg);
  14.  
  15. #define SEEK(fd, pos, errmsg)\
  16.     if (lseek (fd, pos, L_SET) != pos)\
  17.         Primitive_Error (errmsg);
  18.  
  19. SYMTAB *Snarf_Symbols (fp) FILE *fp; {
  20.     int fd;            /* a file descriptor */
  21.     long fdi;            /* a counter for the file desc table */
  22.     pFDR file_desc;        /* pointer to the filedesc table */
  23.     struct filehdr file_hdr;    /* pointer to the file header */
  24.     char *strbase;
  25.     char *strings;
  26.     HDRR sym_hdr;        /* pointer to symbolic header */
  27.     long symi;            /* a counter for the local symbol table */
  28.     pSYMR symbol;        /* pointer to symbol table */
  29.  
  30.     SYMTAB *tab;
  31.     char *p;
  32.     SYM *sp, **nextp;
  33.  
  34.     fd = fileno(fp);
  35.  
  36.     /* rewind the object file, since who knows what it's status is... */
  37.     SEEK(fd, 0, "unable to rewind object file");
  38.  
  39.     /* read in the file header */
  40.     READ(fd, &file_hdr, sizeof(file_hdr), "unable to read file header");
  41.  
  42.     /* seek to the start of the symbolic header */
  43.     SEEK(fd, file_hdr.f_symptr, "unable to seek to symbolic header");
  44.  
  45.     /* read in the symbolic header */
  46.     READ(fd, &sym_hdr, sizeof(sym_hdr), "unable to read symbolic header");
  47.  
  48.     tab = (SYMTAB *)Safe_Malloc (sizeof (SYMTAB));
  49.     tab->first = 0;
  50.     tab->strings = 0;
  51.     nextp = &tab->first;
  52.  
  53.     SEEK(fd, sym_hdr.cbSymOffset, "unable to seek to symbol table");
  54.     symbol = (pSYMR)Safe_Malloc (sym_hdr.isymMax * sizeof (SYMR));
  55.     READ(fd, symbol, (sym_hdr.isymMax * sizeof(SYMR)),
  56.        "unable to read symbol table");
  57.  
  58.     SEEK(fd, sym_hdr.cbSsOffset, "unable to seek to string table");
  59.     strings = Safe_Malloc (sym_hdr.issMax);
  60.     READ(fd, strings, sym_hdr.issMax, "unable to read string table");
  61.  
  62.     SEEK(fd, sym_hdr.cbFdOffset, "unable to seek to file descriptor table");
  63.     file_desc = (pFDR)Safe_Malloc (sym_hdr.ifdMax * sizeof(FDR));
  64.     READ(fd, file_desc, (sym_hdr.ifdMax * sizeof(FDR)),
  65.     "unable to read file descriptor table");
  66.  
  67.     /* for each file in the file descriptor table do:
  68.     */
  69.     for (fdi = 0; fdi < sym_hdr.ifdMax; fdi++) {
  70.     strbase = strings + file_desc[fdi].issBase;
  71.     for (symi = file_desc[fdi].isymBase;
  72.         symi < file_desc[fdi].csym + file_desc[fdi].isymBase;
  73.         symi++) {
  74.         if (symbol[symi].st == stProc && symbol[symi].sc == scText) {
  75.         p = symbol[symi].iss + strbase;
  76.     
  77.         /* allocate another node in the symbol table list */
  78.         sp = (SYM *)Safe_Malloc (sizeof (SYM));
  79.         sp->name = Safe_Malloc (strlen (p) + 1);
  80.     
  81.         /* set the new nodes values */
  82.         strcpy (sp->name, p);
  83.         sp->value = symbol[symi].value;
  84.     
  85.         /* link the node into the linked list */
  86.         *nextp = sp;
  87.         nextp = &sp->next;
  88.         *nextp = 0;
  89.         }
  90.     }
  91.     }
  92.     return tab;
  93. }
  94.  
  95. #ifdef INIT_OBJECTS
  96. SYMTAB *Open_File_And_Snarf_Symbols (name) char *name; {
  97.     FILE *fp;
  98.     SYMTAB *tab;
  99.   
  100.     if ((fp = fopen (name, "r")) == NULL)
  101.     Primitive_Error ("can't open a.out file");
  102.     tab = Snarf_Symbols (fp);
  103.     (void)fclose (fp);
  104.     return tab;
  105. }
  106. #endif /* INIT_OBJECTS */
  107.